package com.wtone.platform.gat.images.gat.utils;

import cn.hutool.core.codec.Base64;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @Author: aiLin
 * @Date: 2020/5/11 17:12
 * @desc: Base64测试
 */
@Slf4j
public class BaseImg {
    public static void main(String[] args) throws Exception {
       String filePath = "F:/img/haiguan/sum.png";
        String savePath = "F:/img/base/img.jpg";
        String imgBase = coImg(filePath);
        String result = toImg(imgBase, savePath);
        log.info(result);
        /*String str = "5e60d596fe804b2eaea1c419e4e8e2b1";
        System.out.println(str.length());*/
    }

    /***
     * 图片转base64编码
     * @return
     */
    public static String coImg(String filePath){
        String encode="";
        try{
            byte[] bytes = Files.readAllBytes(Paths.get(filePath));
            encode = Base64.encode(bytes);
            //log.warn(encode);
        }catch (Exception e){
            e.printStackTrace();
        }
        return encode;
    }

    /***
     * base64字符串转图片
     * @param imgBase
     * @throws Exception
     */
    public static String toImg(String imgBase,String savePath) throws Exception {
        try{
            byte[] decode = java.util.Base64.getDecoder().decode(imgBase);
            FileOutputStream fileOutputStream = new FileOutputStream(new File(savePath));
            fileOutputStream.write(decode);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "success";
    }

    /**
     * 指定图片宽度和高度和压缩比例对图片进行压缩
     * @param imgsrc 源图片地址
     * @param imgdist 目标图片地址
     */
    public static void reduceImg(String imgsrc, String imgdist) {
        try {
            File srcfile = new File(imgsrc);
            // 检查图片文件是否存在
            if (!srcfile.exists()) {
                System.out.println("文件不存在");
            }
            int[] results = getImgWidthHeight(srcfile);

            int widthdist = results[0];
            int heightdist = results[1];
            // 开始读取文件并进行压缩
            Image src = ImageIO.read(srcfile);

            // 构造一个类型为预定义图像类型之一的 BufferedImage
            BufferedImage tag = new BufferedImage( widthdist,  heightdist, BufferedImage.TYPE_INT_RGB);

            // 这边是压缩的模式设置
            tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, Image.SCALE_SMOOTH), 0, 0, null);

            //创建文件输出流
            FileOutputStream out = new FileOutputStream(imgdist);
            //将图片按JPEG压缩，保存到out中
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            encoder.encode(tag);
            //关闭文件输出流
            out.close();
        } catch (Exception ef) {
            ef.printStackTrace();
        }
    }

    /**
     * 获取图片宽度和高度
     *
     * @param file 图片路径
     * @return 返回图片的宽度
     */
    public static int[] getImgWidthHeight(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int result[] = { 0, 0 };
        try {
            // 获得文件输入流
            is = new FileInputStream(file);
            // 从流里将图片写入缓冲图片区
            src = ImageIO.read(is);
            // 得到源图片宽
            result[0] =src.getWidth(null);
            // 得到源图片高
            result[1] =src.getHeight(null);
            is.close();  //关闭输入流
        } catch (Exception ef) {
            ef.printStackTrace();
        }

        return result;
    }




    public static String  getStrHostPeopleId(String str) {
        if(str.contains("-")){
            return str.replace("-","");
        }
        return str;
    }
    public static String  getPeopleId( String str) {
        StringBuilder sb = new StringBuilder(str);//构造一bai个StringBuilder对象
        if(str.length()==32){
            sb.insert(8, "-");//在指定的位置1，插入指定的字符串
            sb.insert(13, "-");
            sb.insert(18, "-");
            sb.insert(23, "-");
            return sb.toString();
        }
        return str;
    }
}
